public class Gaussian implements Sampling {

    private float Gauss(float dist, float sigma) {
	return (float) Math.pow(0.5, Math.pow(dist / sigma, 2));
    }

    private float Dist(float x1, float y1, float x2, float y2) {
	return (float) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    }

    public int[] getSample(ImageAsArray.ImageAsArrayHolder img, float x, float y, float param) {
	param = Math.max(2, (float) 1 / param);
	float sigma = param / 3;
	float factorSum = 0;
	float curFactor = 0;
	int[] sample = { 0, 0, 0, 0 };

	// calculating sum of all factors to normalize
	int fromX = (int) (Math.max((x - param), 0));
	int toX = (int) (Math.min((x + param), img.width));
	int fromY = (int) (Math.max((y - param), 0));
	int toY = (int) (Math.min((y + param), img.height));

	for (int ix = fromX; ix < toX; ix++) {
	    for (int iy = fromY; iy < toY; iy++) {
		factorSum += Gauss(Dist(x, y, ix, iy), sigma);
	    }
	}

	for (int ix = fromX; ix < toX; ix++) {
	    for (int iy = fromY; iy < toY; iy++) {
		curFactor = Gauss(Dist(x, y, ix, iy), sigma) / factorSum;
		for (int i = 1; i < 4; i++) {
		    sample[i] += (int) (img.pixels_ARGB[(iy * img.width + ix) * 4 + i] * curFactor);
		}

	    }
	}

	return sample;
    }
}
